package co.sanduche.teach.gallery;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import co.sanduche.teach.PostActivity;
import co.sanduche.teach.R;
import co.sanduche.util.LoadBitmapTask;
import co.sanduche.util.LoadBitmapTask.ImageHolder;
import co.sanduche.util.TouchImageView;

public class PhotoFragment extends Fragment {

	public static final String IMAGE_ITEM_KEY = "image_item";
	private static final int UPDATE_WAIT_TIME = 5000;
	private ImageButton btnPublish;
	private ViewGroup bottomActionBar;

	private float bottomActionBarOriginalY = -1;

	private ImageItem ii;
	int pos;
	private PhotoActivity parent;
	private Handler mHandler;
	private Runnable updateRunable;

	public PhotoFragment() {
		mHandler = new Handler();

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		pos = getArguments().getInt("pos");
		parent = (PhotoActivity) getActivity();
		ii = parent.get(pos);
	}

	@Override
	public View onCreateView(final LayoutInflater inflater,
			final ViewGroup container, Bundle savedInstanceState) {

		try {
			Log.i(getClass().getSimpleName(), "Dibujando fragmento: " + pos);

			View root = inflater.inflate(R.layout.fragment_photo, container,
					false);
			TouchImageView imageView = (TouchImageView) root
					.findViewById(R.id.photoImgView);
			ProgressBar progressBar = (ProgressBar) root
					.findViewById(R.id.photoProgressBar);
			btnPublish = (ImageButton) root.findViewById(R.id.photoBtnPublish);
			bottomActionBar = (ViewGroup) root
					.findViewById(R.id.bottomActionBar);
			Log.v(getClass().getSimpleName(), "Se va a crear tarea bitmap");
			new LoadBitmapTask(getActivity(), PhotoActivity.PHOTO_SIZE,
					PhotoActivity.PHOTO_SIZE).execute(new ImageHolder(
					imageView, ii.getPath(), progressBar));

			btnPublish.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent i = new Intent(PhotoFragment.this.getActivity(),
							PostActivity.class);
					i.putExtra(IMAGE_ITEM_KEY, ii);
					PhotoFragment.this.getActivity().startActivity(i);

				}
			});

			imageView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					parent.setCurrentFragment(PhotoFragment.this);
					parent.showFullscreen(!parent.isFullscreen());
					animate();
				}
			});
			setVisibility();
			return root;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	protected void animate() {
		if (bottomActionBarOriginalY == -1) {
			int[] location = new int[2];
			bottomActionBar.getLocationOnScreen(location);
			bottomActionBarOriginalY = bottomActionBar.getTop();

		}
		if (parent.isFullscreen())
			animateToFullscreen();
		else
			animateFromFullscreen();
	}

	private void animateToFullscreen() {
		ObjectAnimator oa1 = ObjectAnimator.ofFloat(bottomActionBar, "y",
				bottomActionBarOriginalY,
				parent.getDisplaymetrics().heightPixels);

		AnimatorListener listener = new AnimatorListenerAdapter() {
			@Override
			public void onAnimationEnd(Animator animation) {
				bottomActionBar.setY(bottomActionBarOriginalY);
				bottomActionBar.setVisibility(View.INVISIBLE);
			}
		};

		AnimatorSet as = new AnimatorSet();
		as.playTogether(oa1);
		as.setDuration(PhotoActivity.FULLSCREEN_TRANSITION_TIME);
		as.setInterpolator(new AccelerateInterpolator());
		as.addListener(listener);

		as.start();
	}

	private void animateFromFullscreen() {
		ObjectAnimator oa1 = ObjectAnimator.ofFloat(bottomActionBar, "y",
				parent.getDisplaymetrics().heightPixels,
				bottomActionBarOriginalY);

		AnimatorSet as = new AnimatorSet();
		as.playTogether(oa1);
		as.setDuration(PhotoActivity.FULLSCREEN_TRANSITION_TIME);
		as.setInterpolator(new AccelerateInterpolator());

		as.start();
	}

	public void setVisibility() {

		if (parent.isFullscreen()) {
			// if this fragment was just clicked, do not set visibility, it will
			// be animated
			if (this == parent.getCurrentFragment()) {
				return;
			}
			bottomActionBar.setVisibility(View.INVISIBLE);

		} else {
			bottomActionBar.setVisibility(View.VISIBLE);

		}
	}

	public void update(String text) {
		// Cancel previous post
		mHandler.removeCallbacks(updateRunable);

		// set a new post
		ii.setMessage(text);
		mHandler.postDelayed(updateRunable, UPDATE_WAIT_TIME);
	}

	public static PhotoFragment newInstance(ImageItem ii, int pos,
			PhotoActivity parent) {
		PhotoFragment f = new PhotoFragment();
		Bundle b = new Bundle();
		f.pos = pos;
		b.putInt("pos", pos);
		f.setArguments(b);
		return f;
	}

	public String toString() {
		return "fragment " + pos;
	}
}